package com.atguigu.spzx.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;

import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class validateCodeServiceImpl implements ValidateCodeService {


    @Autowired
    private RedisTemplate<String,String> redisTemplate;
  @Override
    public ValidateCodeVo   generateValidateCode(){
    //生成验证码
    CircleCaptcha circleCaptcha= CaptchaUtil.createCircleCaptcha(125,40,4,2);
    String  codeValue=circleCaptcha.getCode();
    String imageBase64=circleCaptcha.getImageBase64();
    // 验证码 存储
     String key= UUID.randomUUID().toString().replaceAll("_","");
     redisTemplate.opsForValue().set("user:validate"+key,codeValue,5, TimeUnit.MINUTES);
     ValidateCodeVo validateCodeVo=new ValidateCodeVo();
     validateCodeVo.setCodeKey(key);
     validateCodeVo.setCodeValue("data:image/png;base64,"+imageBase64);
      return  validateCodeVo;
  }
}
